# -*- coding: utf-8 -*-

from PyQt4.QtGui import QDialog
from PyQt4.QtCore import SIGNAL
from PyQt4.Qt import QRect, QDesktopWidget
from PyQt4.QtCore import QCoreApplication, QEventLoop
from ui_projectselect import Ui_ProjectSelect
from TrainCsvWindow import TrainCsvWindow
from TestCsvWindow import TestCsvWindow
from subprocess import *
from mainfunctions import *
from githg import *
from cvssvn import *

connection = sqlite.connect('commits.db')
cursor = connection.cursor()

selectedVCS = ""
projectRootUrl = ""
projectModuleUrl = ""
trainRevisionInfo = ""

projectName = ""
projectVersion = 0

class ProjectSelect(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__(self, parent)
        self.ui = Ui_ProjectSelect()
        self.ui.setupUi(self)
        self.browser = None
                
        self.train = TrainCsvWindow()
        self.train.hide()
        
        self.test = TestCsvWindow()
        self.test.hide()
        
        available_geom = QDesktopWidget().availableGeometry()
        current_geom = self.frameGeometry()
        self.setGeometry(available_geom.width() / 2 - current_geom.width() / 2, available_geom.height() / 2 - current_geom.height() / 2, current_geom.width(), current_geom.height())
        
        self.connect(self.ui.projSelectOkBtn, SIGNAL("clicked()"), self.pickProject)
        self.connect(self.ui.projSelectCancelBtn, SIGNAL("clicked()"), self.cancelButton)

    def setSelectedVCS(self, currentVCS):
        global selectedVCS
        selectedVCS = currentVCS

    def setProjectRootUrl(self, currentPath):
        global projectRootUrl
        projectRootUrl = currentPath

    def setProjectModuleUrl(self, currentModuleUrl):
        global projectModuleUrl
        projectModuleUrl = currentModuleUrl         
        
    def setBrowser(self, browser):
        self.browser = browser
        self.train.browser = self.browser
        self.test.browser = self.browser
        
    def cancelButton(self):
        self.hide()        

    def listProjects2(self):
        listProjects2(self.browser)

    def revertRepo(self):
        global selectedVCS
        global projectRootUrl
        global projectModuleUrl
        global trainRevisionInfo
        
        if selectedVCS == "Cvs":
            if projectModuleUrl=="":
                tempArray = projectRootUrl.split("/")
                arrayLen = len(tempArray)
                if tempArray[arrayLen-1] == "":
                    rootDir = tempArray[arrayLen-2]
                elif tempArray[arrayLen-1] != "":
                    rootDir = tempArray[arrayLen-1]
            else:
                tempArray = projectModuleUrl.split("/")
                rootDir = tempArray[0]
            revertCvsRepo(rootDir, trainRevisionInfo, self.browser)
            
        elif selectedVCS == "Git":
            tempArray = projectRootUrl.split("/")
            arrayLen = len(tempArray)
            if tempArray[arrayLen-1] == "":
                rootDir = tempArray[arrayLen-2]
            elif tempArray[arrayLen-1] == "":
                rootDir = tempArray[arrayLen-1]            
            revertGitRepo(rootDir, trainRevisionInfo, self.browser)
            
        elif selectedVCS == "Mercurial":
            tempArray = projectRootUrl.split("/")
            arrayLen = len(tempArray)
            if tempArray[arrayLen-1] == "":
                rootDir = tempArray[arrayLen-2]
            elif tempArray[arrayLen-1] == "":
                rootDir = tempArray[arrayLen-1]            
            revertHgRepo(rootDir, trainRevisionInfo, self.browser)
            
        elif selectedVCS == "Subversion":
            tempArray = projectRootUrl.split("/")
            arrayLen = len(tempArray)
            if tempArray[arrayLen-1] == "":
                rootDir = tempArray[arrayLen-2]
            elif tempArray[arrayLen-1] == "":
                rootDir = tempArray[arrayLen-1]
            revertSvnRepo(rootDir, trainRevisionInfo, self.browser)        

    def pickProject(self):
        if (len(self.ui.projName.text()) != 0) or (len(self.ui.projVersion.text()) != 0):
            projectName = str(self.ui.projName.text())
            projectVersion = str(self.ui.projVersion.text())
            isSuccessful = pickProject(self.ui.projName.text(), self.ui.projVersion.text(), self.browser)

            if(isSuccessful == 5555):
                self.hide()
                isSuccessful = 0
                self.ui.projName.clear()
                self.ui.projVersion.clear()
                
                self.browser.append("\nTrain and Test Csv Files are going to be created...\n\nThese files will be related with:\n\nProject Name: " + projectName + "\tProject Version: " + projectVersion)
                QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
                global selectedVCS
                self.train.setSelectedVCS(selectedVCS)
                global projectRootUrl
                self.train.setProjectRootUrl(projectRootUrl)
                global projectModuleUrl
                self.train.setProjectModuleUrl(projectModuleUrl)

                self.train.exec_()
                global trainRevisionInfo
                trainRevisionInfo = self.train.returnRevisionInfo()

                self.test.exec_()
                self.revertRepo()
